package com.xingu.xg.common.controller;

import com.xingu.xg.common.AddNodeQuery;
import com.xingu.xg.common.CopyNodeQuery;
import com.xingu.xg.common.DeleteNodeQuery;
import com.xingu.xg.common.entity.TreeNode;
import com.xingu.xg.common.service.TreeNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/tree")
@Api(tags = "树形结构管理")
public class TreeNodeController {
    @Autowired
    private TreeNodeService treeNodeService;

    // 1. 查询列表API
    @ApiOperation("查询列表")
    @GetMapping("/list")
    public List<TreeNode> listTreeNodes(@RequestParam(required = false) String parentId) {
        return treeNodeService.listTreeNodes(parentId);
    }

    // 2. 同级向下/向上创建数据API
    @ApiOperation("同级创建数据")
    @PostMapping("/sibling")
    public TreeNode createSiblingNode(
            @RequestBody @Valid AddNodeQuery param,
            @RequestParam String nodeName,
            @RequestParam String operationType) {
        return treeNodeService.createSiblingNode(param, nodeName, operationType);
    }

    // 3. 创建子级和父级API
    @ApiOperation("创建子级和父级")
    @PostMapping("/parent-child")
    public TreeNode createParentChildNode(
            @RequestBody @Valid AddNodeQuery param,
            @RequestParam String nodeName,
            @RequestParam String operationType) {
        return treeNodeService.createParentChildNode(param, nodeName, operationType);
    }

    // 4. 多行数据粘贴API
    @ApiOperation("多行数据粘贴")
    @PostMapping("/copy-paste")
    public List<TreeNode> copyPasteNodes(
            @RequestBody @Valid CopyNodeQuery param,
            @RequestParam String targetId) {
        return treeNodeService.copyPasteNodes(param, targetId);
    }

    // 5. 选中数据删除API
    @ApiOperation("选中数据删除")
    @DeleteMapping("/delete")
    public void deleteNodes(@RequestBody @Valid DeleteNodeQuery param) {
        treeNodeService.deleteNodes(param);
    }


}
